package com.ruoyi.web.controller.system;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.bo.PStoreSeckillBo;
import com.ruoyi.system.domain.vo.PStoreSeckillVo;
import com.ruoyi.system.service.IPStoreSeckillService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 商品秒杀产品Controller
 *
 * @author ruoyi
 * @date 2022-08-08
 */
@Validated
@Api(value = "商品秒杀产品控制器", tags = {"商品秒杀产品管理"})
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/storeSeckill")
public class PStoreSeckillController extends BaseController {

    private final IPStoreSeckillService iPStoreSeckillService;

    /**
     * 查询商品秒杀产品列表
     */
    @ApiOperation("查询商品秒杀产品列表")
    @SaCheckPermission("system:storeSeckill:list")
    @GetMapping("/list")
    public TableDataInfo<PStoreSeckillVo> list(PStoreSeckillBo bo, PageQuery pageQuery) {
        return iPStoreSeckillService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出商品秒杀产品列表
     */
    @ApiOperation("导出商品秒杀产品列表")
    @SaCheckPermission("system:storeSeckill:export")
    @Log(title = "商品秒杀产品", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(PStoreSeckillBo bo, HttpServletResponse response) {
        List<PStoreSeckillVo> list = iPStoreSeckillService.queryList(bo);
        ExcelUtil.exportExcel(list, "商品秒杀产品", PStoreSeckillVo.class, response);
    }

    /**
     * 获取商品秒杀产品详细信息
     */
    @ApiOperation("获取商品秒杀产品详细信息")
    @SaCheckPermission("system:storeSeckill:query")
    @GetMapping("/{id}")
    public R<PStoreSeckillVo> getInfo(@ApiParam("主键")
                                     @NotNull(message = "主键不能为空")
                                     @PathVariable("id") Long id) {
        return R.ok(iPStoreSeckillService.queryById(id));
    }

    /**
     * 新增商品秒杀产品
     */
    @ApiOperation("新增商品秒杀产品")
    @SaCheckPermission("system:storeSeckill:add")
    @Log(title = "商品秒杀产品", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody PStoreSeckillBo bo) {
        return toAjax(iPStoreSeckillService.insertByBo(bo) ? 1 : 0);
    }

    /**
     * 修改商品秒杀产品
     */
    @ApiOperation("修改商品秒杀产品")
    @SaCheckPermission("system:storeSeckill:edit")
    @Log(title = "商品秒杀产品", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody PStoreSeckillBo bo) {
        return toAjax(iPStoreSeckillService.updateByBo(bo) ? 1 : 0);
    }

    /**
     * 删除商品秒杀产品
     */
    @ApiOperation("删除商品秒杀产品")
    @SaCheckPermission("system:storeSeckill:remove")
    @Log(title = "商品秒杀产品", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@ApiParam("主键串")
                                       @NotEmpty(message = "主键不能为空")
                                       @PathVariable Integer[] ids) {
        return toAjax(iPStoreSeckillService.deleteWithValidByIds(Arrays.asList(ids), true) ? 1 : 0);
    }
}
